package com.mrper.util;

import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;

import com.miguo.app.AppContext;
import com.miguo.utils.CommonUtils;

import java.lang.reflect.Field;

/**
 * Created by admin on 2016/3/16.
 */
public class AnimationUtil {

    /**
     * 获取去顶部的按钮的显示的动画
     * @return 去顶部的按钮的显示的动画
     */
    public static Animation getGo2TopButton2ShowAnimation(){
        TranslateAnimation mTopUpAnima = new TranslateAnimation(0.0F, 0.0F,CommonUtils.dip2px(AppContext.getContext(), 48.0F), 0.0F);
        mTopUpAnima.setDuration(500L);
        return mTopUpAnima;
    }

    /**
     * 获取去顶部的按钮的隐藏的动画
     * @return 去顶部的按钮的隐藏的动画
     */
    public static Animation getGo2TopButton2HideAnimation(){
        TranslateAnimation mTopDownAnima = new TranslateAnimation(0.0F, 0.0F, 0.0F, CommonUtils.dip2px(AppContext.getContext(), 48.0F));
        mTopDownAnima.setDuration(500L);
        return mTopDownAnima;
    }

    /**
     * 执行Indictor动画的函数
     * @param handler 要执行的上下文对象
     * @param inidctorView 要执行操作的view
     * @param recordIndexFiledName 记录当前索引位置的字段名称
     * @param currentTabPosition 当前要去的索引值
     * @param percent indictor控件占他父控件的比重
     * @param during 整个动画花费的时间
     */
    public static void startIndictorTranslationAnimation(Object handler, View inidctorView,String recordIndexFiledName,int currentTabPosition,float percent,int during){
        try {
            Class objCls = handler.getClass();
            Field recordField = objCls.getDeclaredField(recordIndexFiledName);
            if(recordField!=null){
                recordField.setAccessible(true);
                int fieldValue = recordField.getInt(handler);
                if(fieldValue == currentTabPosition) return;//当前索引无变化
                TranslateAnimation translateAnimation = new TranslateAnimation(
                        TranslateAnimation.RELATIVE_TO_PARENT, fieldValue * percent,
                        TranslateAnimation.RELATIVE_TO_PARENT, currentTabPosition * percent,
                        TranslateAnimation.RELATIVE_TO_PARENT, 0, TranslateAnimation.RELATIVE_TO_PARENT, 0);
                translateAnimation.setFillAfter(true);
                translateAnimation.setInterpolator(new LinearInterpolator());
                translateAnimation.setDuration(during);
                inidctorView.startAnimation(translateAnimation);
                recordField.setInt(handler,currentTabPosition);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 执行Indictor动画的函数,默认动画时间200毫秒
     * @param handler 要执行的上下文对象
     * @param inidctorView 要执行操作的view
     * @param recordIndexFiledName 记录当前索引位置的字段名称
     * @param currentTabPosition 当前要去的索引值
     * @param percent indictor控件占他父控件的比重
     */
    public static void startIndictorTranslationAnimation(Object handler, View inidctorView,String recordIndexFiledName,int currentTabPosition,float percent){
        startIndictorTranslationAnimation(handler,inidctorView,recordIndexFiledName,currentTabPosition,percent,200);
    }


}
